import Vue from 'vue'
import VueRouter from 'vue-router'
import auth from '@/utils/token'
import { Message } from 'element-ui'
// import GeekUser from '@/views/GeekUser.vue'
// import GeekLogin from '@/views/GeekLogin.vue'
// import GeekHome from '@/views/GeekHome.vue'
// import GeekPublish from '@/views/GeekPublish.vue'
// import GeekLagout from '@/views/GeekLagout.vue'
// import GeekArticle from '@/views/GeekArticle.vue'

const GeekLogin = () => import('@/views/GeekLogin.vue')

const GeekHome = () => import('@/views/GeekHome.vue')
const GeekPublish = () => import('@/views/GeekPublish.vue')
const GeekLagout = () => import('@/views/GeekLagout.vue')
const GeekArticle = () => import('@/views/GeekArticle.vue')
const GeekUser = () => import('@/views/GeekUser.vue')

Vue.use(VueRouter)
const router = new VueRouter({
  mode: 'history',
  routes: [
    { path: '/login', component: GeekLogin },
    {
      path: '/',
      component: GeekLagout,
      children: [
        { path: '', component: GeekHome },
        { path: 'publish/:id?', component: GeekPublish },
        { path: 'article', component: GeekArticle },
        { path: 'user', component: GeekUser }
      ]
    }
  ]
})

router.beforeEach((to, from, next) => {
  const token = auth.getToken()
  if (to.path !== '/login' && !token) {
    Message.error('登录信息失效')
    return next('/login')
  }
  next()
})

export default router
